package com.ruoyi.web.controller.system.app;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.UtilTool;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.IAppUserNoticeService;
import com.ruoyi.system.service.IAppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IAppNoticeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 通知Controller
 * 
 * @author ruoyi
 * @date 2021-07-30
 */
@Controller
@RequestMapping("/system/notice")
public class AppNoticeController extends BaseController
{
    private String prefix = "system/notice";

    @Autowired
    private IAppNoticeService appNoticeService;
    @Autowired
    private IAppUserService appUserService;
    @Autowired
    private IAppUserNoticeService appUserNoticeService;

    /**
     * 查询通知列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AppNotice appNotice)
    {
        checkNotice(new AppNotice());
        startPage();
        List<AppNotice> list = appNoticeService.selectAppNoticeList(appNotice);
        return getDataTable(list);
    }

    /**
     * 导出通知列表
     */
    @Log(title = "通知", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AppNotice appNotice)
    {
        List<AppNotice> list = appNoticeService.selectAppNoticeList(appNotice);
        ExcelUtil<AppNotice> util = new ExcelUtil<AppNotice>(AppNotice.class);
        return util.exportExcel(list, "通知数据");
    }

    /**
     * 新增保存通知
     */
    @Log(title = "通知", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AppNotice appNotice)
    {
        // 获取当前的用户名称
        String username = SecurityUtils.getUsername();
        appNotice.setCreateSysUser(username);
        appNotice.setCreateTime(new Date(System.currentTimeMillis()));
        appNotice.setUpdateTime(new Date(System.currentTimeMillis()));
        appNoticeService.insertAppNotice(appNotice);
        return AjaxResult.success(appNotice);
    }

    /**
     * 修改保存通知
     */
    @Log(title = "通知", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AppNotice appNotice)
    {
        appNotice.setUpdateTime(new Date(System.currentTimeMillis()));
        return toAjax(appNoticeService.updateAppNotice(appNotice));
    }

    /**
     * 发送
     */
    @PostMapping("/send")
    @ResponseBody
    public AjaxResult send(AppNotice appNotice) {
        String username = SecurityUtils.getUsername();
        appNotice.setState(1);
        appNotice.setSendSysUser(username);
        appNotice.setUpdateTime(new Date(System.currentTimeMillis()));
        appNoticeService.updateAppNotice(appNotice);
        List<AppUser> appUsers = appUserService.selectAppUserList(new AppUser());
        for (AppUser user:appUsers){
            AppUserNotice appUserNotice = new AppUserNotice();
            // type 0默认注册用户 1手机唯一码用户
            if (user.getType()==0){
                appUserNotice.setUserName(user.getUserName());
            }else {
                appUserNotice.setUuid(user.getUuid());
            }
            appUserNotice.setNoticeId(appNotice.getId());
            appUserNoticeService.insertAppUserNotice(appUserNotice);
        }
        return AjaxResult.success();
    }

    /**
     * 删除通知
     */
    @Log(title = "通知", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        String[] id = ids.split(",");
        for (int i=0;i<id.length;i++){
            AppUserNotice appUserNotice = new AppUserNotice();
            appUserNotice.setNoticeId(Long.valueOf(id[i]));
            appUserNoticeService.del(appUserNotice);
        }
        return toAjax(appNoticeService.deleteAppNoticeByIds(ids));
    }

    //添加AppUserNotice记录
    private void addAppUserNotice(long noticeId){
        List<AppUser> appUsers = appUserService.selectAppUserList(new AppUser());
        for (AppUser user:appUsers){
            AppUserNotice appUserNotice = new AppUserNotice();
            // type 0默认注册用户 1手机唯一码用户
            if (user.getType()==0){
                appUserNotice.setUserName(user.getUserName());
            }else {
                appUserNotice.setUuid(user.getUuid());
            }
            appUserNotice.setNoticeId(noticeId);
            appUserNoticeService.insertAppUserNotice(appUserNotice);
        }
    }

    /**
     * 查询是否有没有更新的通知
     * @param appNotice
     */
    private void checkNotice(AppNotice appNotice){
        appNotice.setState(0);
        List<AppNotice> list = appNoticeService.selectAppNoticeList(appNotice);
        for (AppNotice notice:list){
            if (!UtilTool.isNull(notice.getEffectTime())){
                if (notice.getEffectTime().getTime()<System.currentTimeMillis()){
                    notice.setState(1);
                    notice.setUpdateTime(new Date(System.currentTimeMillis()));
                    appNoticeService.updateAppNotice(notice);
                    addAppUserNotice(notice.getId());
                }
            }
        }
    }
}
